package com.ebc.eservice.service.cardStat;

import com.ebc.eservice.mapper.InteractClientMapper;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.MyTimeUtil;
import com.ebc.eservice.vo.card.CardStatReqVo;
import com.ebc.eservice.vo.card.Client.ClientStat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Joey
 * @Description:名片-客户记录业务
 * @date:2022/9/28 0:09
 */
@Service
public class CardClientImp implements ClientInteractService{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private InteractClientMapper clientMapper;

    @Override
    public JsonResult getClientStat(Integer cardId) {
        MyTimeUtil timeUtil = new MyTimeUtil();
        String today = timeUtil.getToDay();
        List<String> sevenDay = timeUtil.getSevenDay();
        List<String> sevenDayEx = timeUtil.getSevenDayExYear();
        //HyperLoglog
        HyperLogLogOperations<String, String> clientVo = redisTemplate.opsForHyperLogLog();
        List<Long> clientList = new ArrayList<>();
        for (int i = 0; i < sevenDay.size(); i++) {
            clientList.add(clientVo.size("cardclient:"+cardId+":date:"+sevenDay.get(i)));
            clientVo.union("total:"+cardId, "cardclient:"+cardId+":date:"+sevenDay.get(i));
        }

        ClientStat tc = clientMapper.getTotalClient(cardId);
        int totalClients = tc.getInteracts();

        HashMap<String, Object> map = new HashMap<>();
        map.put("cardTotalClient",totalClients);//名片总客户
        map.put("todayClient",clientVo.size("cardclient:"+cardId+":date:"+today));//今日访客
        map.put("UTimeList",sevenDayEx);//近7天时间列表
        map.put("UVisitTotalNum",clientVo.size("total:"+cardId));  //近7天访客总数
        map.put("UperDayData",clientList);  //近7天每日数据
        return JsonResult.success(map);
    }

    @Override
    public JsonResult getAllClientStat(Integer cardId) {
        List<ClientStat> clientStatList = clientMapper.getAllClientStat(cardId);
        return JsonResult.success(clientStatList);
    }

    @Override
    public JsonResult getClientStatByPhone(Integer cardId, String clientPhone) {
        List<ClientStat> clientStatList = clientMapper.getClientStatByPhone(cardId, clientPhone);
        return JsonResult.success(clientStatList);
    }
}
